home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / blank / bserver_v14.lha / BServer_v1.4 / Sources.lha / Sources / clients / Shade.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-23  |  8.9 KB  |  314 lines

  1. ;/*
  2. sc RESOPT DATA=NEAR UCHAR CONSTLIB STREQ NMINC STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE IGNORE=73 Shade.c
  3. slink from LIB:c.o Shade.o to //Clients/Shade LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD STRIPDEBUG NOICONS
  4. delete Shade.o
  5. quit
  6.  
  7.  Shade 1.0  (Client for BServer)
  8.  
  9.  Copyright © 1994-1995 Stefano Reksten of 3AM - The Three Amigos!!!
  10.  All rights reserved.
  11.  
  12.  This was asked by Massimo Capanni. Why couldn't HE make it? :-)
  13. */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17. #include <graphics/gfxbase.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/intuition.h>
  21. #include <proto/graphics.h>
  22. #include <clib/alib_protos.h>
  23. #include <time.h>
  24.  
  25. #include "/include/client.h"
  26.  
  27. char *ver = "$VER: Shade 1.0 "__AMIGADATE__;
  28.  
  29. struct IntuitionBase *IntuitionBase;
  30. struct GfxBase *GfxBase;
  31. struct DisplayIDInformation *dinfo;
  32.  
  33. UWORD __chip plasma_mask[58] = {
  34.  0x1F, 0xC000,
  35.  0xFF, 0xF800,
  36.  0x1FF, 0xFC00,
  37.  0x7FF, 0xFF00,
  38.  0xFFF, 0xFF80,
  39.  0x1FFF, 0xFFC0,
  40.  0x1FFF, 0xFFC0,
  41.  0x3FFF, 0xFFE0,
  42.  0x7FFF, 0xFFF0,
  43.  0x7FFF, 0xFFF0,
  44.  0x7FFF, 0xFFF0,
  45.  0xFFFF, 0xFFF8,
  46.  0xFFFF, 0xFFF8,
  47.  0xFFFF, 0xFFF8,
  48.  0xFFFF, 0xFFF8,
  49.  0xFFFF, 0xFFF8,
  50.  0xFFFF, 0xFFF8,
  51.  0xFFFF, 0xFFF8,
  52.  0x7FFF, 0xFFF0,
  53.  0x7FFF, 0xFFF0,
  54.  0x7FFF, 0xFFF0,
  55.  0x3FFF, 0xFFE0,
  56.  0x1FFF, 0xFFC0,
  57.  0x1FFF, 0xFFC0,
  58.  0xFFF, 0xFF80,
  59.  0x7FF, 0xFF00,
  60.  0x1FF, 0xFC00,
  61.  0xFF, 0xF800,
  62.  0x1F, 0xC000
  63.  };
  64.  
  65. UWORD __chip plasmaplanes[8][58];
  66. UWORD __chip carryplanes[8][58];
  67.  
  68. #define BALLDIM 0x1D
  69.  
  70. extern ULONG RangeSeed;
  71.  
  72. struct Screen *scr;
  73. UWORD swidth, sheight;
  74. UBYTE sdepth;
  75. struct BitMap *scrbmap;
  76. struct RastPort rp;
  77. struct BitMap plasmabmap, carrybmap, maskbmap, tempplasmabmap, tempcarrybmap, tempscrbmap;
  78. WORD X, Y, velX, velY;
  79.  
  80.  
  81. void Moveplasma( void )
  82. {
  83. UBYTE n;
  84.  
  85. /* plasma movement... OK this part is a load of crap :-) */
  86.  
  87. X += velX;
  88. if ( X >= swidth - BALLDIM )
  89.     {
  90.     X = swidth - BALLDIM;
  91.     velX = RangeRand( 6 ) - 8;
  92.     }
  93. if ( X < 0 )
  94.     {
  95.     X = 0;
  96.     velX = RangeRand( 6 ) + 3;
  97.     }
  98.  
  99. Y += velY;
  100. if ( Y >= sheight - BALLDIM )
  101.     {
  102.     Y = sheight - BALLDIM;
  103.     velY = RangeRand( 6 ) - 8;
  104.     }
  105. if ( Y < 0 )
  106.     {
  107.     Y = 0;
  108.     velY = RangeRand( 6 ) + 3;
  109.     }
  110.  
  111. /*
  112.     And now... a bit of plasma THEORY... by GUNDAM of 3AM !!! :-)
  113.  
  114.     How can plasma be generated? Well. Suppose we are working in a 32
  115.     colors screen. We have 6 bitplanes. We order the colors in crescent
  116.     order, so the color number 0 will be the darkest, the color number 31
  117.     will be the brightest. All we have to do is simply this: when we
  118.     draw the ball... we must increase by one the color of the pixels
  119.     "under" the ball. «Yes, we knew it!» Okie-dokie, just wait a bit! :-)
  120.  
  121.     Consider this: HOW can we increase the colors under the ball? It is
  122.     NOT possible to ReadPixel then increase the color then WritePixel
  123.     again! Let's use the blitter => all we have then is to increase a
  124.     number using the blitter. Consider e.g. we have this pixel color
  125.     under the ball: 00010111. We have to get to the number 00011000. Of
  126.     course the blitter is not an ALU so we just have to simulate it. :-)
  127.     We must increase the pixel IF the corresponding bit in the mask is 1.
  128.     So for the first bit let's use the mask. For the other bits, we will
  129.     have to increase them ONLY if the previous bit generated a carry.
  130.     Let's define the function that generates the SUM: (BNC|NBC), where
  131.     B indicates the source, C the destination, NB and NC their negated
  132.     values. The function that calculates the CARRY is (BC). To prove that
  133.     this will work, let's increase the previous 00010111 number.
  134.  
  135.     76543210   Bit 0 is (11), because 1 was in the mask. So sum is (10|01)
  136.     CCCCCCCM   that is 0; carry is (11) that is 1. To calculate bits from
  137.     00010111   1 to 7 we will use the carry. Bit 1 is (10|01) = 0, carry (11) 
  138.     SSSSSSSS   is 1. Bit 2 is (10|01) = 0, carry (11) = 1. Bit 3 is (11|00)
  139.                which is 1, while carry is (10) = 0. From here, having 0 in
  140.     the carry, sums will have the B's bit value and 0 for the carry, as the
  141.     sum will be (1B|0NB) => B and the carry will be (0B) => 0. So we DID
  142.     increase a number using the blitter! Is this magic? No: a man's magic
  143.     is another man's (ME :-) engineering! It took me nearly 5 minutes to
  144.     get to this solution!
  145.  
  146.     All we have to do now is to put that value back.
  147.  
  148.     So all we have to do is
  149.     1) a first blit using the screen's bitmap and the plasma mask to a
  150.        "sum" place using the SUM function.
  151.     2) a second blit using the screen's bitmap and the plasma mask to a
  152.        "carry" place using the CARRY function.
  153.     3) FOR all the remaining planes DO
  154.            blit the carry and the sum in the sum using SUM
  155.            blit the carry and the sum in the carry using CARRY
  156.  
  157.     Believe me or not... This was HARD to write USING THE OS!!! ;-)
  158.     All those double blits could be avoided BUMPING directly in the hardware!
  159.  
  160. */
  161.  
  162. tempscrbmap.Planes[0] = scrbmap->Planes[0];
  163.  
  164. tempplasmabmap.Planes[0] = plasmabmap.Planes[0];
  165. BltBitMap( &maskbmap, 0, 0, &tempplasmabmap, 0, 0, BALLDIM, BALLDIM, (ABC|ABNC), 0xFF, NULL ); /* VANILLA */
  166. BltBitMap( &tempscrbmap, X, Y, &tempplasmabmap, 0, 0, BALLDIM, BALLDIM, (ANBC|ABNC), 0xFF, NULL ); /* SUM */
  167.  
  168. tempcarrybmap.Planes[0] = carrybmap.Planes[1];
  169. BltBitMap( &maskbmap, 0, 0, &tempcarrybmap, 0, 0, BALLDIM, BALLDIM, (ABC|ABNC), 0xFF, NULL ); /* VANILLA */
  170. BltBitMap( &tempscrbmap, X, Y, &tempcarrybmap, 0, 0, BALLDIM, BALLDIM, (ABC), 0xFF, NULL ); /* CARRY */
  171.  
  172. for ( n = 1; n < sdepth; n++ )
  173.     {
  174.     tempscrbmap.Planes[0] = scrbmap->Planes[n];
  175.  
  176.     tempplasmabmap.Planes[0] = plasmabmap.Planes[n];
  177.     tempcarrybmap.Planes[0] = carrybmap.Planes[n]; /* just temp */
  178.     BltBitMap( &tempcarrybmap, 0, 0, &tempplasmabmap, 0, 0, BALLDIM, BALLDIM, (ABC|ABNC), 0xFF, NULL ); /* VANILLA */
  179.     BltBitMap( &tempscrbmap, X, Y, &tempplasmabmap, 0, 0, BALLDIM, BALLDIM, (ANBC|ABNC), 0xFF, NULL ); /* SUM */
  180.  
  181.     if ( n < sdepth - 1 )
  182.         {
  183.         tempcarrybmap.Planes[0] = carrybmap.Planes[n+1];
  184.         tempplasmabmap.Planes[0] = carrybmap.Planes[n]; /* same here */
  185.         BltBitMap( &tempplasmabmap, 0, 0, &tempcarrybmap, 0, 0, BALLDIM, BALLDIM, (ABC|ABNC), 0xFF, NULL ); /* VANILLA */
  186.         BltBitMap( &tempscrbmap, X, Y, &tempcarrybmap, 0, 0, BALLDIM, BALLDIM, (ABC), 0xFF, NULL ); /* CARRY */
  187.         }
  188.     }
  189.  
  190. /* Unfortunately these are needed to avoid flickering... :-( */
  191. BltBitMap( scrbmap, X, Y, &carrybmap, 0, 0, BALLDIM, BALLDIM, 0xC0, 0xFF, NULL );
  192. BltMaskBitMapRastPort( &plasmabmap, 0, 0, &rp, 0, 0,
  193.         BALLDIM, BALLDIM, (ABC|ABNC|ANBC), (PLANEPTR)plasma_mask );
  194. WaitTOF();
  195. BltBitMap( &carrybmap, 0, 0, scrbmap, X, Y, BALLDIM, BALLDIM, 0xC0, 0xFF, NULL );
  196. }
  197.  
  198.  
  199. void Blank( void )
  200. {
  201. struct Rectangle *rect;
  202. UWORD n;
  203. UBYTE brightness;
  204. ULONG displayID;
  205.  
  206. rect = GETTXTOSCANRECT(dinfo);
  207. swidth = RECTANGLEWIDTH(rect);
  208. sheight = RECTANGLEHEIGHT(rect);
  209. displayID = DISPLAYID( dinfo );
  210. brightness = GETBRIGHTNESS(dinfo);
  211.  
  212. if ( CheckAA() )
  213.     sdepth = 8;
  214. else
  215.     {
  216.     sdepth = 5;
  217.  
  218.     if ( displayID & HIRES )
  219.         {
  220.         displayID &= ~HIRES;
  221.         swidth >>= 1;
  222.         }
  223.     if ( displayID & SUPERHIRES )
  224.         {
  225.         displayID &= ~SUPERHIRES;
  226.         swidth >>= 1;
  227.         }
  228.     }
  229.  
  230. if ( scr = OpenScreenTags( NULL,
  231.     SA_Width, swidth,
  232.     SA_Height, sheight,
  233.     SA_Depth, sdepth,
  234.     SA_Quiet, TRUE,
  235.     SA_DisplayID, displayID,
  236.     TAG_END ) )
  237.     {
  238.     register struct ViewPort *vp = &(scr->ViewPort);
  239.  
  240.     scrbmap = scr->RastPort.BitMap;
  241.     SpritesOff();
  242.  
  243.     X = RangeRand( swidth - BALLDIM );
  244.     Y = RangeRand( sheight - BALLDIM );
  245.     velX = RangeRand( 8 ) - 16; if ( !velX ) velX = 1;
  246.     velY = RangeRand( 8 ) - 16; if ( !velY ) velY = 1;
  247.  
  248. if ( CheckAA() )
  249.     {
  250.     for ( n = 0; n < 64; n++ )
  251.         SetRGB32( vp, n, 0, 0, (n*brightness/100)<<25);
  252.     for ( n = 0; n < 64; n++ )
  253.         SetRGB32( vp, n+64, 0, (n*brightness/100)<<24, (63*brightness/100)<<25 );
  254.     for ( n = 0; n < 64; n++ )
  255.         SetRGB32( vp, n+128, (n*brightness/100)<<23, (63*brightness/100)<<24, ((63-n)*brightness/100)<<25 );
  256.     for ( n = 0; n < 64; n++ )
  257.         SetRGB32( vp, n+192, ((63-n)*brightness/100)<<23, ((63-n)*brightness/100)<<24, 0 );
  258.     }
  259. else
  260.     {
  261.     for ( n = 0; n < 16; n++ )
  262.         SetRGB4( vp, n, 0, 0, n*brightness/100 );
  263.     for ( n = 16; n < 32; n++ )
  264.         SetRGB4( vp, n, 0, (n-15)*brightness/100, 15*brightness/100 );
  265.     }
  266.  
  267.     InitBitMap( &plasmabmap, sdepth, BALLDIM, BALLDIM );
  268.     InitBitMap( &carrybmap, sdepth, BALLDIM, BALLDIM );
  269.     for ( n = 0; n < sdepth; n++ )
  270.         {
  271.         plasmabmap.Planes[n] = (PLANEPTR)plasmaplanes[n];
  272.         carrybmap.Planes[n] = (PLANEPTR)carryplanes[n];
  273.         }
  274.  
  275.     InitBitMap( &maskbmap, 1, BALLDIM, BALLDIM );
  276.     maskbmap.Planes[0] = (PLANEPTR)plasma_mask;
  277.  
  278.     InitBitMap( &tempplasmabmap, 1, BALLDIM, BALLDIM );
  279.     InitBitMap( &tempcarrybmap, 1, BALLDIM, BALLDIM );
  280.     InitBitMap( &tempscrbmap, 1, swidth, sheight );
  281.     /* No need for planes as these will be changed... */
  282.  
  283.     InitRastPort( &rp );
  284.     rp.BitMap = &carrybmap;
  285.  
  286.     while( STILL_BLANKING )
  287.         Moveplasma();
  288.  
  289.     CloseScreen( scr );
  290.     SpritesOn();
  291.     }
  292. else
  293.     SendClientMsg( ACTION_FAILED );
  294. }
  295.  
  296.  
  297. void __main( char *line )
  298. {
  299. if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
  300.     {
  301.     if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
  302.         {
  303.         if ( dinfo = OpenCommunication() )
  304.             {
  305.             RangeSeed = time( NULL );
  306.             Blank();
  307.             CloseCommunication( dinfo );
  308.             }
  309.         CloseLibrary( (struct Library *)GfxBase );
  310.         }
  311.     CloseLibrary( (struct Library *)IntuitionBase );
  312.     }
  313. }
  314.